﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;
using WuDada.Core.Generic.Util;

public partial class case_in : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private ITagService m_TagService;
    private ConfigHelper m_ConfigHelper = new ConfigHelper();
    private SessionHelper m_SessionHelper = new SessionHelper();

    //熱門案件 NodeId=8
    private int m_NodeId = 8;
    private int m_PostId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_TagService = m_PostFactory.GetTagService();
        m_PostId = QueryStringHelper.GetInteger("id", 0);

        if (!IsPostBack)
        {
            hdnId.Value = m_PostId.ToString();
            FillVO();
            FillRepeater();
            SetControl();
        }
    }

    private void SetControl()
    {
        ////收藏
        bool showCollection = false;
        bool showCancelCollection = false;
        bool showBid = false;

        if (m_SessionHelper.Member != null)
        {
            if (CountCollection() > 0)
            {
                showCancelCollection = true;
            }
            else
            {
                showCollection = true;
            }

            PostVO postVO = m_PostService.GetPostById(int.Parse(hdnId.Value));
            {
                if (postVO != null)
                {
                    if (DateTime.Now >= postVO.ShowDate && DateTime.Now < postVO.CloseDate.Value.AddDays(1))
                    {
                        showBid = true;
                    }
                }
            }
        }

        aCollection.Visible = showCollection;
        aCancelCollection.Visible = showCancelCollection;
        pnlBidUI.Visible = showBid;
    }

    private int CountCollection()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("TagType", ((int)TagVO.Type.Favorite).ToString());
        conditions.Add("OnwerId", m_SessionHelper.Member.MemberId.ToString());
        conditions.Add("TargetId", hdnId.Value);
        int tagCount = m_TagService.GetTagCount(conditions);
        return tagCount;
    }

    private void AddViewer(PostVO postVO)
    {
        postVO.Viewer += 1;
        m_PostService.UpdatePost(postVO);
    }

    private void FillVO()
    {
        PostVO postVO = m_PostService.GetPostById(m_PostId);
        if (postVO != null)
        {
            AddViewer(postVO);
            UIHelper.FillUI(pnlUI, postVO);
            lblActDate.Text = string.Format("{0}~{1}", postVO.ActDateStart.Value.ToString("yyyy/MM/dd"), postVO.ActDateEnd.Value.ToString("yyyy/MM/dd"));
            if (postVO.Price <= 0)
            {
                RangeValidator1.MinimumValue = Convert.ToInt32(postVO.StartPrice).ToString();
                lblPrice.Text = string.Format("起標價：{0}", String.Format("{0:0,0}", postVO.StartPrice));
            }
            else
            {
                RangeValidator1.MinimumValue = Convert.ToInt32(postVO.Price + 1).ToString();
                lblPrice.Text = string.Format("最高出價：{0}", String.Format("{0:0,0}", postVO.Price));
            }
            lblBidDate.Text = string.Format("{0}~{1}", postVO.ShowDate.Value.ToString("yyyy.MM.dd"), postVO.CloseDate.Value.ToString("yyyy.MM.dd"));
            imgPicFileName.ImageUrl = string.Format("upload/{0}", postVO.PicFileName);

            Dictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("NodeId", m_NodeId.ToString());
            conditions.Add("Flag", "1");
            conditions.Add("ParentPostId", hdnId.Value);

            IList<PostVO> subList = m_PostService.GetPostList(conditions);
            if (subList != null && subList.Count > 0)
            {
                StringBuilder sbStore = new StringBuilder();

                foreach (PostVO sub in subList)
                {
                    sbStore.AppendFormat("{0}，", sub.Title);
                }

                lblCustomField3.Text = sbStore.ToString().TrimEnd('，');
            }

            if (subList == null)
            {
                subList = new List<PostVO>();
            }
            subList.Insert(0, postVO);
            RepeaterImg.DataSource = subList;
            RepeaterImg.DataBind();
        }
    }

    private void FillRepeater()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", m_NodeId.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("WithOutSubPost", "1");

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        Repeater1.DataSource = m_PostService.GetPostList(conditions);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillRepeater();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            string detailUrl = string.Format("{0}/case_in.aspx?id=", m_ConfigHelper.Host, postVO.PostId);

            UIHelper.SetHyperLinkNUrl(ref ctrl, "aFB", string.Format("http://www.facebook.com/share.php?u={0}", detailUrl));
            UIHelper.SetHyperLinkNUrl(ref ctrl, "aGoogle", string.Format("https://plus.google.com/share?url={0}", detailUrl));
            UIHelper.SetHyperLinkNUrl(ref ctrl, "aTwitter", string.Format("http://www.twitter.com/share?url={0}", detailUrl));
        }
    }

    private void UpdateCollection()
    {
        PostVO postVO = m_PostService.GetPostById(int.Parse(hdnId.Value));
        if (postVO != null)
        {
            Dictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("TagType", ((int)TagVO.Type.Favorite).ToString());
            conditions.Add("TargetId", hdnId.Value);

            postVO.Collection = m_TagService.GetTagCount(conditions);
            m_PostService.UpdatePost(postVO);
        }
    }

    protected void aCollection_Click(object sender, EventArgs e)
    {
        if (m_SessionHelper.Member != null)
        {
            if (CountCollection() == 0)
            {
                TagVO tagVO = new TagVO()
                {
                    OnwerId = m_SessionHelper.Member.MemberId.ToString(),
                    TargetId = hdnId.Value,
                    TagType = TagVO.Type.Favorite
                };
                m_TagService.CreateTag(tagVO);
                UpdateCollection();
                SetControl();
            }
        }
    }

    protected void aCancelCollection_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("TagType", ((int)TagVO.Type.Favorite).ToString());
        conditions.Add("OnwerId", m_SessionHelper.Member.MemberId.ToString());
        conditions.Add("TargetId", hdnId.Value);
        IList<TagVO> tags = m_TagService.GetTagList(conditions);
        if (tags != null && tags.Count > 0)
        {
            m_TagService.DeleteTag(tags[0]);
            UpdateCollection();
            SetControl();
        }
    }

    protected void aBidding_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("case_in_bid.aspx?id={0}&bid={1}", hdnId.Value, txtBidPrice.Text.Trim()));
    }
}